package com.ibiaoke.goods.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.goods.entity.ShopPrice;
import com.ibiaoke.goods.entity.XcxShopPrice;
import com.ibiaoke.goods.entity.XcxShopPriceExample;
import com.ibiaoke.goods.mapper.XcxShopPriceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public class XcxShopPriceDao extends BaseDao<XcxShopPrice,XcxShopPriceExample,Integer,XcxShopPriceMapper> {
    @Autowired
    private XcxShopPriceMapper xcxShopPriceMapper;

    @Override
    public XcxShopPriceMapper getMapper() {
        return xcxShopPriceMapper;
    }

    public List<XcxShopPrice> listShopPrice(List<Integer> XcxGoodsIds, Integer shopId) {
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdIn(XcxGoodsIds).andShopIdEqualTo(shopId);
        return selectByExample(example);
    }

    public void updateXcxGoodStockNum(Integer goodId,Integer shopId,Integer num){
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodId).andShopIdEqualTo(shopId);
        XcxShopPrice xcxShopPrice= selectByExample(example).get(0);
        xcxShopPrice.setStockNum(xcxShopPrice.getStockNum()+num);
        updateByPrimaryKeySelective(xcxShopPrice);
    }

    public Integer updateXcxGoodStockNum1(Integer goodId,Integer shopId,Double num){
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodId).andShopIdEqualTo(shopId);
        XcxShopPrice xcxShopPrice= selectByExample(example).get(0);
        Double s  = new BigDecimal(xcxShopPrice.getStockNum().toString()).add(new BigDecimal(num)).doubleValue();
        xcxShopPrice.setStockNum(s);
        return updateByPrimaryKeySelective(xcxShopPrice);
    }

    public void updatePtXcxGoodStockNum(Integer goodId,Integer shopId,Integer count){
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodId).andShopIdEqualTo(shopId);
        XcxShopPrice xcxShopPrice= selectByExample(example).get(0);
        xcxShopPrice.setStockNum(xcxShopPrice.getStockNum()-count);
        updateByPrimaryKeySelective(xcxShopPrice);
    }
    public int delShopPrice(Integer xcxGoodsId,Integer shopId) {
        XcxShopPriceExample example = new XcxShopPriceExample();
        XcxShopPriceExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(xcxGoodsId);
        if (shopId != null) {
            criteria.andShopIdEqualTo(shopId);
        }
        return deleteByExample(example);
    }

    public List<XcxShopPrice> listShopPrices(Integer xcxGoodsId) {
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(xcxGoodsId);
        return selectByExample(example);
    }

    public void updateXcxShopPriceStockNum(Integer goodId,Integer goodNum,Integer shopId){
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodId).andShopIdEqualTo(shopId);
        XcxShopPrice xcxShopPrice= selectByExample(example).get(0);
        xcxShopPrice.setStockNum(xcxShopPrice.getStockNum() - goodNum);
        updateByPrimaryKeySelective(xcxShopPrice);
    }

    public XcxShopPrice findGood(Integer outShopId,Integer goodId){
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodId).andShopIdEqualTo(outShopId);
        return selectByExample(example).get(0);
    }

    public List<XcxShopPrice> listShopPricesByShopId(Integer goodsId, Integer shopId) {
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId).andShopIdEqualTo(shopId);
        return selectByExample(example);
    }

    public List<XcxShopPrice> listByIds(List<Integer> goodsIds) {
        XcxShopPriceExample example = new XcxShopPriceExample();
        example.createCriteria().andGoodsIdIn(goodsIds);
        return selectByExample(example);
    }
}



